﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Qcy.Jshj.Web.Models;

namespace Qcy.Jshj.Web.Helpers
{
    public class DataHelper : EnvironmentBase
    {
        /// <summary>
        /// 获取资讯列表数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static IEnumerable<Article> GetArticles(int pageIndex, int pageSize, out int count)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                count = context.Article.Count();
                return (from q in context.Article
                        orderby q.Id descending
                        select new Article()
                        {
                            Id = q.Id,
                            Title = IsChinese ? q.Title : q.TitleEn,
                            CreationDate = q.CreationDate
                        }).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
            }
        }

        /// <summary>
        /// 获取一条资讯内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Article GetArticle(int id)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                var article = context.Article.FirstOrDefault(s => s.Id == id);
                if (article != null)
                    return new Article()
                    {
                        Id = article.Id,
                        Title = IsChinese ? article.Title : article.TitleEn,
                        Contents = IsChinese ? article.Contents : article.ContentsEn,
                        CreationDate = article.CreationDate
                    };
                throw new HttpException(404, "");
            }
        }

        /// <summary>
        /// 获取产品列表数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static IEnumerable<Qcy.Jshj.Web.Helpers.Product> GetProducts(int pageIndex, int pageSize, out int count)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                count = context.Product.Count();
                var source = (from q in context.Product
                              orderby q.Id descending
                              select q).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
                var result = new List<Product>();
                foreach (var q in source)
                {
                    result.Add(new Product()
                                   {
                                       Id = q.Id,
                                       Title = IsChinese ? q.Title : q.TitleEn,
                                       CreationDate = q.CreationDate,
                                       Image = string.IsNullOrEmpty(q.Pictures) ? "" : q.Pictures.Split(',').ElementAt(0)
                                   });
                }
                return result;
            }
        }

        /// <summary>
        /// 获取产品列表数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static IEnumerable<Qcy.Jshj.Web.Helpers.Product> GetIndexProducts(int pageIndex, int pageSize, out int count)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                count = context.Product.Count();
                var source = (from q in context.Product
                              where q.IndexShow
                              orderby q.Id descending
                              select q).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
                var result = new List<Product>();
                foreach (var q in source)
                {
                    result.Add(new Product()
                    {
                        Id = q.Id,
                        Title = IsChinese ? q.Title : q.TitleEn,
                        CreationDate = q.CreationDate,
                        Image = string.IsNullOrEmpty(q.Pictures) ? "" : q.Pictures.Split(',').ElementAt(0)
                    });
                }
                return result;
            }
        }


        /// <summary>
        /// 根据大分类和二级分类获取产品
        /// </summary>
        /// <param name="id"></param>
        /// <param name="cate"></param>
        /// <returns></returns>
        public static IEnumerable<Product> GetProducts(int id, int cate)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                IEnumerable<Models.Product> queryable;
                if (id <= 0) id = GetDefaultCategoryId(context);
                if (cate == 0)
                    queryable = from q in context.Product.Include("Category").Include("Category.Parent")
                                where q.Category.Parent.Id == id || q.Category.Id == id
                                orderby q.Id descending
                                select q;
                else
                    queryable = from q in context.Product.Include("Category").Include("Category.Parent")
                                where q.Category.Id == cate
                                orderby q.Id descending
                                select q;
                var list = new List<Product>();
                foreach (var product in queryable)
                {
                    Product item = new Product
                    {
                        Id = product.Id,
                        Title = IsChinese ? product.Title : product.TitleEn,
                        CreationDate = product.CreationDate,
                        Image = string.IsNullOrEmpty(product.Pictures) ? "" : product.Pictures.Split(new char[] { ',' }).ElementAt<string>(0),
                        Contents = IsChinese ? product.Contents : product.ContentsEn
                    };
                    list.Add(item);
                }
                return list;
            }
        }

        public static Article Article(int id)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                var article = context.Article.FirstOrDefault(s => s.Id == id);
                return new Article()
                {
                    Id = article.Id,
                    Contents = IsChinese ? article.Contents : article.ContentsEn,
                    CreationDate = article.CreationDate,
                    Title = IsChinese ? article.TitleEn : article.TitleEn
                };
            }
        }

        /// <summary>
        /// 获取公司信息的列表数据
        /// </summary>
        /// <param name="infoType">公司信息类型</param>
        public static IEnumerable<CompanyInfo> GetCompanyInfos(CompanyInfoType infoType)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                return context.CompanyInfo.Where(s => s.InfoType == infoType && s.MenuShow)
                              .Select(s => new CompanyInfo()
                              {
                                  Id = s.Id,
                                  Contents = IsChinese ? s.Content : s.ContentEn,
                                  Title = IsChinese ? s.Name : s.NameEn

                              }).ToList();
            }
        }

        /// <summary>
        /// 根据Id获取一个公司信息
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static CompanyInfo CompanyInfo(int id, CompanyInfoType type)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                var c = context.CompanyInfo.FirstOrDefault(s => s.Id == id && s.InfoType == type);
                if (c == null)
                    return CompanyInfo(type);
                return new CompanyInfo()
                {
                    Id = c.Id,
                    Title = IsChinese ? c.Name : c.NameEn,
                    Contents = IsChinese ? c.Content : c.ContentEn
                };
            }
        }

        /// <summary>
        /// 返回一个默认公司信息
        /// </summary>
        /// <param name="ctype"></param>
        /// <returns></returns>
        public static CompanyInfo CompanyInfo(CompanyInfoType ctype)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                var c = context.CompanyInfo.FirstOrDefault(s => s.InfoType == ctype);
                if (c == null)
                    throw new HttpException(404, "");
                return new CompanyInfo()
                {
                    Id = c.Id,
                    Title = IsChinese ? c.Name : c.NameEn,
                    Contents = IsChinese ? c.Content : c.ContentEn
                };
            }
        }

        /// <summary>
        /// 获取产品分类的一级分类列表数据
        /// </summary>
        public static IEnumerable<CompanyInfo> GetProductCategories()
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                return context.ProductCategory.Where(s => s.Parent == null)
                              .Select(s => new CompanyInfo()
                              {
                                  Id = s.Id,
                                  Contents = IsChinese ? s.Description : s.DescriptionEn,
                                  Title = IsChinese ? s.Name : s.NameEn
                              }).ToList();
            }
        }

        /// <summary>
        /// 获取产品分类的二级分类列表数据
        /// </summary>
        public static IEnumerable<CompanyInfo> GetProductCategories(int id)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                if (id <= 0) id = GetDefaultCategoryId(context);
                return (from s in context.ProductCategory.Include("Parent")
                        where s.Parent.Id == id
                        select new CompanyInfo
                        {
                            Id = s.Id,
                            Contents = IsChinese ? s.Description : s.DescriptionEn,
                            Title = IsChinese ? s.Name : s.NameEn
                        }).ToList();
            }
        }
        private static int GetDefaultCategoryId(JshjDataContext context)
        {
            ProductCategory category = context.ProductCategory.FirstOrDefault<ProductCategory>();
            if (category == null) throw new HttpException(0x194, "");
            return category.Id;
        }


        /// <summary>
        /// 获取分类信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static CompanyInfo ProductCategory(int id)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                var c = context.ProductCategory.FirstOrDefault(s => s.Id == id);
                if (c == null)
                    c = context.ProductCategory.FirstOrDefault();
                if (c == null)
                    throw new HttpException(404, "");
                return new CompanyInfo()
                {
                    Id = c.Id,
                    Contents = IsChinese ? c.Description : c.DescriptionEn,
                    Title = IsChinese ? c.Name : c.NameEn
                };
            }
        }

        public static void AddOrder(Order order)
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                context.Order.Add(order);
                context.SaveChanges();
            }
        }


        public static IEnumerable<CompanyInfo> GetFriendLinks()
        {
            using (JshjDataContext context = new JshjDataContext())
            {
                return context.Link.Select(s => new CompanyInfo()
                              {
                                  Id = s.Id,
                                  Contents = s.Url,
                                  Title = s.Name
                              }).ToList();
            }
        }
    }


    public abstract class ShowBase
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Contents { get; set; }
    }


    public class Article : ShowBase
    {
        public DateTime CreationDate { get; set; }
    }

    public class CompanyInfo : ShowBase
    {
    }

    public class Product : ShowBase
    {
        public string Image { get; set; }
        public DateTime CreationDate { get; set; }
    }
}